package com.oocl.crm.controller.impl;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;

import com.oocl.crm.controller.Controller;
import com.oocl.crm.parser.CmdParser;
import com.oocl.crm.parser.impl.CmdParserImpl;
import com.oocl.crm.pojo.Customer;
import com.oocl.crm.util.FormatCmd;
import com.oocl.crm.util.FormatPrinter;

public class ControllerImpl implements Controller{
	private CmdParser parser;
	
	public ControllerImpl() {
		parser = new CmdParserImpl();
	}

	@Override
	public void commandProcess(InputStream in, OutputStream out) {
		//完成键盘命令输入接收
		Scanner scaner=new Scanner(in);
		while(scaner.hasNext()){
			String cmd=scaner.nextLine();
			//初步解析，并调用CmdParser
			
			//调用FomartPrinter打印输出
			String action=null;
			action = FormatCmd.cmdKind(cmd);
			switch (action) {
			case "L":
				Customer[] cs= parser.find(cmd);
				FormatPrinter.printCustomers(cs, out);
				break;
			case "A":
				parser.processAdd(cmd);
				break;
			case "D":
				parser.processDelete(cmd);
				break;
			case "U":
				parser.processUpdate(cmd);
				break;
			case "O":
				Customer[] cs2 = parser.processSort(cmd);
				FormatPrinter.printCustomers(cs2, out);
				break;
			case "H":
				parser.processHelp(cmd);
				break;
			case "Q":
				System.exit(0);
			default:
				break;
			}
			System.out.println("\n Pls input your command -->");
		}
		scaner.close();	
	}
}
